package com.roadjava.demo08.service;

import com.roadjava.demo08.util.ValidationUtil8;

import javax.validation.constraints.NotNull;
import java.lang.reflect.Method;
import java.util.List;

public class UserInfoService {

    public static void main(String[] args) {
        UserInfoService service = new UserInfoService();

        service.getByName("Tom");
        service.getByName(null);
    }

    /**
     * 方法非bean类型的入参校验
     * 1. 方法参数前加注解
     * 2. 执行入参校验，建议用aop编程的思想来实现
     */
    public String getByName(@NotNull String name) {
        // 建议用反射来实现
        StackTraceElement st = Thread.currentThread().getStackTrace()[1];
        String methodName = st.getMethodName(); // 获取当前调用的方法名:getByName
        Method method = null;
        try {
            method = this.getClass().getDeclaredMethod(methodName, String.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<String> list = ValidationUtil8.validNotBean(this, method, new Object[]{name});

        System.out.println(list);
        return "ok";
    }
}
